package org.xa3apm.vote.hibernate.entities;

import java.io.Serializable;
import java.util.Collection;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.OneToMany;
import javax.persistence.Table;

@Entity
@Table(name = "tSettlement")
@NamedQueries({
		@NamedQuery(name = "Settlement.getById", query = "SELECT S FROM Settlement S WHERE S.id = :id"),
		@NamedQuery(name = "Settlement.getByName", query = "SELECT S FROM Settlement S WHERE S.name = :name"),
		@NamedQuery(name = "Settlement.getAll", query = "SELECT S FROM Settlement S"),
		@NamedQuery(name = "Settlement.getByMunicipalityId", query = "SELECT S FROM Settlement S WHERE S.municipality = :municipality "),
// @NamedQuery(name="Settlement.update",query="UPDATE Settlement S SET S.name= :newName WHERE S.id = :id")
})
public class Settlement implements Serializable {
	/**
	 * 
	 */
	private static final long serialVersionUID = 6685990237505611479L;
	@Id
	@GeneratedValue
	@Column(name = "id", nullable = false, unique = true)
	private Long id;
	@Column(name = "name")
	private String name;
	@ManyToOne(targetEntity = Municipality.class)
	private Municipality municipality;
	@OneToMany(mappedBy="settlements")
	private Collection<Address> addresses;

	// SQL query as Strings
	public Settlement() {
		// TODO Auto-generated constructor stub
	}

	public Settlement(String name, Municipality municipality,
			Collection<Address> addresses) {
		super();
		this.name = name;
		this.municipality = municipality;
		this.addresses = addresses;
	}

	/**
	 * @return the settlement_id
	 */
	public final Long getId() {
		return id;
	}

	/**
	 * @param settlementId
	 *            the settlement_id to set
	 */
	public final void setId(Long sId) {
		this.id = sId;
	}

	/**
	 * @return the settlementName
	 */
	public final String getName() {
		return name;
	}

	/**
	 * @param settlementName
	 *            the settlementName to set
	 */
	public final void setName(String sName) {
		this.name = sName;
	}

	/**
	 * @return the municipality
	 */
	public final Municipality getMunicipality() {
		return municipality;
	}

	/**
	 * @param municipality
	 *            the municipality to set
	 */
	public final void setMunicipality(Municipality municipality) {
		this.municipality = municipality;
	}

	public Collection<Address> getAddresses() {
		return addresses;
	}

	public void setAddresses(Collection<Address> addresses) {
		this.addresses = addresses;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result
				+ ((addresses == null) ? 0 : addresses.hashCode());
		result = prime * result + ((id == null) ? 0 : id.hashCode());
		result = prime
				* result
				+ ((municipality == null) ? 0 : municipality.getId().hashCode());
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof Settlement)) {
			return false;
		}
		Settlement other = (Settlement) obj;
		if (addresses == null) {
			if (other.addresses != null) {
				return false;
			}
		} else if (!addresses.equals(other.addresses)) {
			return false;
		}
		if (id == null) {
			if (other.id != null) {
				return false;
			}
		} else if (!id.equals(other.id)) {
			return false;
		}
		if (municipality == null) {
			if (other.municipality != null) {
				return false;
			}
		} else if (!municipality.getId().equals(other.municipality.getId())) {
			return false;
		}
		if (name == null) {
			if (other.name != null) {
				return false;
			}
		} else if (!name.equals(other.name)) {
			return false;
		}
		return true;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		return id + ", " + name + " " + municipality.getId();
	}

}
